package work20241121;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author zdl
 * @date 2024/11/21
 */
public class Demo2 {
    public static void main(String[] args) {
        /**
         * 水果售卖管理系统
         * 1. 显示所有水果 2. 添加水果 3. 删除水果  4. 修改水果信息 5. 售卖水果 6. 退出
         * 请选择操作: 1
         * 所有水果信息：
         * 名称: 苹果      价格: 5.50 库存: 100
         * 名称: 香蕉      价格: 3.20 库存: 80
         * 名称: 橙子      价格: 6.00 库存: 50
         */
        //水果
        String[] fruit = {"苹果","香蕉","橙子"};
        String[] fruit1 = new String[fruit.length+1];
        for (int i = 0; i < fruit.length; i++) {
            fruit1[i] = fruit[i];
        }
        //价格
        double[] price = {5.50,3.20,6.00};
        double[] price1= new double[price.length+1];
        for (int i = 0; i < price.length; i++) {
            price1[i] = price[i];
        }
        //库存
        int[] inventory = {100,80,50};
        int[] inventory1 = new int[inventory.length+1];
        for (int i = 0; i < inventory.length; i++) {
            inventory1[i] = inventory[i];
        }
        Scanner sc =new Scanner(System.in);
        boolean isRunning = true;
        while (isRunning){
            System.out.println("欢迎进入水果售卖管理系统(请选择操作1-6):");
            System.out.println("1. 显示所有水果");
            System.out.println("2. 添加水果");
            System.out.println("3. 删除水果");
            System.out.println("4. 修改水果信息");
            System.out.println("5. 售卖水果");
            System.out.println("6. 退出");
            System.out.println("请选择操作:");
            int choice = sc.nextInt();
            switch (choice){
                case 1:
                    System.out.println("显示所有水果:");
                    for (int i = 0; i < fruit1.length; i++) {
                        System.out.print("名称:"+fruit1[i]+"\t");
                        System.out.print("价格:"+price1[i]+"\t");
                        System.out.println("库存:"+inventory1[i]+"\t");
                    }
                    break;
                case 2:
                    System.out.println("添加水果");
                    System.out.println("请输入添加水果名称:");
                    String fruitName = sc.next();



                    fruit1[fruit1.length-1] = fruitName;
                    System.out.println("请输入添加"+fruitName+"的价格:");
                    double priceName = sc.nextDouble();


                    price1[price1.length-1] = priceName;
                    System.out.println("请输入添加"+fruitName+"的库存:");
                    int inventoryName = sc.nextInt();


                    inventory1[inventory1.length-1] = inventoryName;
                    System.out.println("添加水果成功！！！");
                    for (int i = 0; i < fruit1.length; i++) {
                        System.out.print("名称:"+fruit1[i]+"\t");
                        System.out.print("价格:"+price1[i]+"\t");
                        System.out.println("库存:"+inventory1[i]+"\t");
                    }
                    break;
                case 3:
                    System.out.println("删除水果");
                    System.out.println("请选择删除水果名称:");
                    String str = sc.next();
                    for (int i = 0; i < fruit1.length; i++) {
                        if(str.equals(fruit1[i])){
                            String[] delFruit = new String[fruit1.length - 1];
                            int delIndex = i;
                            //System.out.println("================================"+delIndex);
                            for (int j = delIndex; j <fruit1.length-1 ; j++) {
                                fruit1[j] = fruit1[j+1];
                            }
                            for (int j = 0; j < fruit1.length-1; j++) {
                                delFruit[j] = fruit1[j];
                            }
                            //System.out.println("==================="+Arrays.toString(delFruit));
                            double[] delPrice = new double[price1.length -1];
                            for (int j = delIndex; j <price1.length-1 ; j++) {
                                price1[j] = price1[j+1];
                            }
                            for (int j = 0; j < price1.length-1; j++) {
                                delPrice[j] = price1[j];
                            }
                            //System.out.println("==================="+Arrays.toString(delPrice));
                            int[] delInventory = new int[inventory1.length-1];
                            for (int j = delIndex; j <inventory1.length-1 ; j++) {
                                inventory1[j] = inventory1[j+1];
                            }
                            for (int j = 0; j < inventory1.length-1; j++) {
                                delInventory[j] = inventory1[j];
                            }
                            //System.out.println("==================="+Arrays.toString(delInventory));
                            for (int j = 0; j < delFruit.length; j++) {
                                System.out.print("名称:"+delFruit[j]+"\t");
                                System.out.print("价格:"+delPrice[j]+"\t");
                                System.out.println("库存:"+delInventory[j]+"\t");
                            }
                            break;
                        } else {
                            System.out.println("没有该类水果，无法删除");
                            break;
                        }
                    }
                    break;
                case 4:
                    System.out.println("修改水果信息");
                    System.out.println("请选择修改水果信息:");
                    String update = sc.next();
                    int updateIndex = 0;
                    for (int i = 0; i < fruit1.length; i++) {
                        if(update.equals(fruit1[i])){
                            updateIndex = i;
                        }
                    }
                    boolean flag = true;
                    while (flag){
                        System.out.println("原价格为:"+price1[updateIndex]+",修改价格为：");
                        double updatePrice = sc.nextDouble();
                        price1[updateIndex] = updatePrice;
                        System.out.println("请问是否修改库存(1:修改   2:不修改)");
                        int num = sc.nextInt();
                        switch (num){
                            case 1:
                                System.out.println("原库存数量为:"+inventory1[updateIndex]+",请填写修改库存数量:");
                                int updateInventory = sc.nextInt();
                                inventory1[updateIndex] = updateInventory;
                                break;
                            case 2:
                                System.out.println("你选择的不修改库存数量！！");
                                flag = false;
                                break;
                        }
                    }
                    System.out.println("修改后的水果信息:");
                    for (int j = 0; j < fruit1.length; j++) {
                        System.out.print("名称:"+fruit1[j]+"\t");
                        System.out.print("价格:"+price1[j]+"\t");
                        System.out.println("库存:"+inventory1[j]+"\t");
                    }
                    break;
                case 5:
                    System.out.println("售卖水果");
                    System.out.println("请选择购买水果名称:");
                    String buyFruit = sc.next();
                    int buyIndex = 0;
                    for (int i = 0; i < fruit.length; i++) {
                        if(buyFruit.equals(fruit[i])){
                            fruit[i] = buyFruit;
                            buyIndex = i;
                        }
                        System.out.println("当前水果库存量为:"+inventory1[buyIndex]+",请输入购买量:");
                        int buyNum = sc.nextInt();
                        if (buyNum < inventory1[buyIndex]){
                            inventory1[buyIndex] -= buyNum;
                            System.out.println("购买后的水果信息:");
                            for (int j = 0; j < fruit1.length; j++) {
                                System.out.print("名称:"+fruit1[j]+"\t");
                                System.out.print("价格:"+price1[j]+"\t");
                                System.out.println("库存:"+inventory1[j]+"\t");
                            }
                            break;
                        }else {
                            System.out.println("库存不足！！！");
                            break;
                        }
                    }

                    break;
                case 6:
                    System.out.println("退出系统，谢谢使用！");
                    isRunning = false;
                    break;
                default:
                    System.out.println("输入无效数字重新输入！！！");
            }
        }



    }
}
